<template>
    <div>
        <!-- body -->
    </div>
</template>
<style scoped>
.form-box {
    /*width: 800px;*/
    width: 90%;
}

.el-select {
    margin-right: 10px;
}

</style>
<script>
// tabsComponent
import indexMenu from '../tabs/IndexMenu.vue'
export default {
    components: {
        indexMenu
    },
    data: function() {
        return {
            options: [], // 级联选择科目源
            props: { // 级联元素设置默认关联信息
                value: 'Name',
                label: 'Name',
                children: 'Childs'
            },
            delivery: false, // 配置状态
            isSubjected: true, // 配置是否可打开 ==> 默认不能打开
            isChangeSubject: false, // 是否显示功能选项
            selectedOptions: [], // 选择的级联科目
            selectAppName: '未选择科目', // 选择的科目tips
            activeName: '1', // 当前标签页（默认为首页）
            editableTabs: [], // tab选项参数数组
            tabsModule: {}, // 所有功能模块
            treeTabModule: {}, // 所有树形模块
            selectApps: [], // 所有科目中最后一级Childs科目
            parentTabsModules: [], // 所有功能模块重新组合
            selectAppObj: {} // 最后选择的科目信息
        }
    },
    methods: {
        /**
         * 选择/改变科目触发事件
         * @method   handleChange
         * @Author   杨少波
         * @DateTime 2017-10-31
         * @Changed  无
         * @param    {string}     value 选择的科目
         */
        handleChange(value) {
            let _self = this;
            // var selectName = _self.selectedOptions[_self.selectedOptions.length - 1];
            let selectName = value[value.length - 1];
            _self.editableTabs.splice(0, _self.editableTabs.length);
            _self.selectAppName = '未选择科目';
            _self.delivery = false;
            // _self.isSubjected = true;
            _self.isSubjected = false;
            _self.openChangeSubject(false);
            _self.activeName = '1';
            _self.options.forEach(function(item) {
                if (item.Name === selectName) {
                    _self.selectAppObj = item;
                    return;
                }
                if (item.Name === value[0]) {
                    _self.selectSubjectName(item.Childs, selectName);
                }
            });
            // _self.options.forEach(function(item) {
            //     if (item.Name === selectName) {
            //         _self.selectAppObj = item;
            //     }
            // })
            _self.selectApps.forEach(function(item) {
                if (item.Name === selectName) {
                    _self.selectAppObj = item;
                    _self.selectAppObj.isSubject = true;
                }
            });
            if (value.length < 1 || _self.isSubjected) {
                _self.isSubjected = true;
                return;
            } else {
                _self.selectAppName = selectName;
            }
        },
        selectSubjectName(opt, name) {
            var _that = this;
            opt.forEach(function(item) {
                if (item.Name === name) {
                    _that.selectAppObj = item;
                } else if (item.Childs) {
                    _that.selectSubjectName(item.Childs, name);
                }
            });
        },
        /**
         * 配置按钮改变事件
         * @method   openChangeSubject
         * @Author   杨少波
         * @DateTime 2017-10-31
         * @Changed  无
         * @param    {boolean}          value  是否打开了配置
         */
        openChangeSubject(value) {
            this.isChangeSubject = value;
            if (value) {
                this.handleClick();
            }
        },
        /**
         * 初始化标签页
         * @method   handleClick
         * @Author   杨少波
         * @DateTime 2017-10-31
         * @Changed  无
         */
        handleClick() {
            if (this.isChangeSubject) {
                let treeTabModules = [];
                let eventabModules = [];
                this.tabsModule.forEach(function(item) {
                    if (item.parentID !== 0) {
                        item.colors = 'twoLevelSub';
                        eventabModules.push(item);
                    } else {
                        treeTabModules.push(item);
                        item.colors = 'oneLevelSub';
                    }
                });
                treeTabModules.forEach(function(item) {
                    item.children = [];
                    eventabModules.forEach(function(tags) {
                        if (tags.parentID === item.id) {
                            item.isShowClick = false;
                            item.children.push(tags);
                        }
                    })
                });
                this.treeTabModule = treeTabModules;
                this.editableTabs.splice(0, this.editableTabs.length);
                this.editableTabs.push({
                    title: '首页',
                    name: '1',
                    max: 9
                }, {
                    title: '课程',
                    name: '2',
                    max: 5
                }, {
                    title: '练习',
                    name: '3',
                    max: 5
                }, {
                    title: '测评',
                    name: '4',
                    max: 5
                });
            }
        },
        /**
         * 获取所有科目信息 && 获取所有功能信息
         * @method   getModule
         * @Author   杨少波
         * @DateTime 2017-10-31
         * @Changed  无
         */
        getModule() {
            let _that = this;
            // 获取所有科目
            let subJectUrl = _that.G_AjaxApi + "/api/softMenu/getNew?agentCode=888";
            // 获取所有功能模块
            let ModuleUrl = _that.G_AjaxApiTest + "/api/function/getFunctionList";

            if (!!window.sessionStorage) {
                var appMenu = sessionStorage.getItem('AppMenuData');
            }
            if (appMenu) {
                _that.options = JSON.parse(appMenu).Childs;
            } else {
                _that.$axios.get(subJectUrl).then(function(response) {
                        _that.options = JSON.parse(response.data.data).Childs;
                        sessionStorage.setItem('AppMenuData', response.data.data);
                    })
                    .catch(function(err) {
                        _that.$alert(err, '接口错误，请联系后台解决！', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.$message({
                                    type: 'info',
                                    message: `action: ${ action }`
                                });
                            }
                        });
                    });;
            }

            _that.$axios.get(ModuleUrl).then(function(response) {
                    _that.tabsModule = response.data.data;
                })
                .catch(function(err) {
                    _that.$alert(err, '接口错误，请联系后台解决！', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'info',
                                message: `action: ${ action }`
                            });
                        }
                    });
                });
        },
        /**
         * 筛选最后一级科目目录
         * @method   getAppJson
         * @Author   杨少波
         * @DateTime 2017-10-31
         * @Changed  无
         * @param    {array}   jsonvalue  所有科目信息
         */
        getAppJson(jsonvalue) {
            var that = this;
            jsonvalue.forEach(function(value) {
                if (value.Childs) {
                    that.getAppJson(value.Childs);
                } else {
                    that.selectApps.push(value);
                }
            });
        },
        /**
         * 初始化页面信息
         * @method   onLoad
         * @Author   杨少波
         * @DateTime 2017-10-31
         * @Changed  无
         * @return   {[type]}   [description]
         */
        onLoad() {
            const users = '管理员';
            if (!!localStorage) {
                let userName = localStorage.getItem('ms_username');
                if (!userName || userName !== 'admin') {
                    this.$router.push('/');
                    return;
                }
            }
            this.$notify({
                title: '尊敬的' + users + ',您好',
                message: '这里是功能配置选项，请谨慎操作！',
                type: 'warning'
            });
            this.getModule();
        }
    },
    created: function() {
        this.onLoad();
    },
    mounted: function() {
        this.getAppJson(this.options);
    }
}

</script>
